package com.lin.thirdPartyBean;

import com.alibaba.druid.pool.DruidDataSource;
import com.lin.dao.BookDao;
import com.lin.service.BookService;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import javax.sql.DataSource;

@Component
public class ThirdPartyBean {
    /**
     * dataSource不需要注入BookDao和BookService, 这里只是演示可以用这种方式注入<br>
     * &nbsp;&nbsp;使用了@Bean注解, 可以省略@Autowired注解,<br>
     * 如下面注入BookDao和BookService时并没有使用@Autowired注解
     */
    @Bean("dataSource")
    public DataSource dataSource (
            @Value("${driver}") String driver,
            @Qualifier("bookDao")BookDao bookDao,
            BookService bookService
            ) {
        try(DruidDataSource dataSource = new DruidDataSource())
        {
            System.out.println("DataSource: " + driver);
            System.out.println("DataSource: " + bookDao);
            System.out.println("DataSource: " + bookService);
            return dataSource;
        }

    }
}
